﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Cache;
using Yd.Util;
using Yd.Model;
using System.Data;
using System.Web.Script.Serialization;
namespace yedian
{
    public partial class ActionEdit : AdminPage
    {
        public string imgUrl = "",tickTable="";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet ds = commdal.Gettz_news(Request["Id"]);
                hdid.Value = Request["Id"];
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    this.fNewsTitle.Value = ds.Tables[0].Rows[0]["actionName"].ToString().Trim();
                    this.NewsContent.Value = ds.Tables[0].Rows[0]["actionContent"].ToString().Trim();
                    this.date1.Text = ds.Tables[0].Rows[0]["startTime"].ToString().Trim();
                    this.date2.Text = ds.Tables[0].Rows[0]["endTime"].ToString().Trim();
                    this.actionAddress.Value = ds.Tables[0].Rows[0]["actionAddress"].ToString().Trim();
                    this.facePic.Value = ds.Tables[0].Rows[0]["actionImgUrl"].ToString().Trim();
                    this.actionTickSM.Value = ds.Tables[0].Rows[0]["ticketInformation"].ToString().Trim();
                    imgUrl = ds.Tables[0].Rows[0]["actionImgUrl"].ToString().Trim();
                    DataSet dst = commdal.Gettz_tick(Request["Id"]);
                    if (dst != null && dst.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow rw in dst.Tables[0].Rows)
                        {
                            tickTable += String.Format(@"<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>", rw["ticketName"].ToString(), rw["fare"].ToString(), rw["allVotes"].ToString(), rw["endTime"].ToString());
                        }
                    }
                    else {
                        tickTable = "<tr><td>早鸟票</td><td>50</td><td>100</td><td>2018-10-30</td></tr>";
                    }
                    return;
                }
                Response.Redirect("AdminIndex.aspx");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            yd_action fnews = new yd_action();
            fnews.actionName= fNewsTitle.Value.Trim();
            fnews.actionContent = NewsContent.Value.Trim();
            fnews.actionAddress = this.actionAddress.Value.Trim();
            fnews.hdId = int.Parse(hdid.Value);
            if (fnews.hdId == 0) {
                errinfo.Text = "编辑失败，请稍后再试！";
                return;
            }
            fnews.startTime = date1.Text.Trim();
            fnews.endTime = date2.Text;
            fnews.actionImgUrl = facePic.Value;
            fnews.ticketInformation = actionTickSM.Value.Trim();
            decimal feiyong = 0;
            fnews.actionCost = feiyong;
            int _fresult = commdal.UpdateAction(fnews);
            if (_fresult != 0)
            {
                errinfo.Text = "修改" + fNewsTitle.Value.Trim() + "失败";
                return;
            }
            string json = tickJson.Value.Trim();
            JavaScriptSerializer Serializer = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
            List<yd_tick> tickList = Serializer.Deserialize<List<yd_tick>>(json);
            if (tickList == null || tickList.Count == 0)
            {
                errinfo.Text = "请先设置活动票";
                return;
            }
            commdal.SaveOrUpdateSql(String.Format(@"DELETE FROM [dbo].[yd_ticket] WHERE hdId={0}", fnews.hdId.ToString()));
            foreach (yd_tick model in tickList)
            {
                commdal.SaveOrUpdateSql(String.Format(@"INSERT INTO [dbo].[yd_ticket]([hdId],[ticketName],[fare],[endTime],[allVotes])VALUES({0},'{1}',{2},'{3}',{4})", fnews.hdId, model.name, model.pice, model.time, model.num));
            }
            errinfo.Text = fNewsTitle.Value.Trim() + "修改成功";
            imgUrl = facePic.Value;
            DataSet dst = commdal.Gettz_tick(fnews.hdId.ToString());
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow rw in dst.Tables[0].Rows)
                {
                    tickTable += String.Format(@"<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td></tr>", rw["ticketName"].ToString(), rw["fare"].ToString(), rw["allVotes"].ToString(), rw["endTime"].ToString());
                }
            }
        }

        public bool IsNumeric(string value)
        {
            bool isDec = true;
            try
            {
                Convert.ToDecimal(value);
            }
            catch
            {
                isDec = false;
            }
            return isDec;
        }
    }
}